'''
from tm1650 import TM1650
display = TM1650(22,21) # clk dio
display.displayDot(2,1) # 显示小数点.位置2 显示1 0关闭
display.displayBit(2,1) # 显示数字。位置2 数字1
display.ShowNum(11) 显示数字

'''
from machine import Pin
class TM1650:
    # TM1650定义
    ADDR_DIS = 0x48  #模式命令

    # 数字:0~9
    NUM = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f] 
    DIG = [0x6e,0x6c,0x6a,0x68]
    DOT = [0,0,0,0]


    DisplayCommand = (0 & 0xfe)+1
    def __init__(self, clk,dio):
        self.clkPin = clk
        self.dioPin = dio
        self.clk = Pin(clk,Pin.OUT)
        self.dio = Pin(dio,Pin.OUT)
        

    def writeByte(self,wr_data):
        clk = self.clk
        dio = self.dio
        for i in range(8):
            if(wr_data & 0x80 == 0x80):
                dio.value(1)
            else:
                dio.value(0)
            clk.value(0)
            time.sleep(0.0001)
            clk.value(1)
            time.sleep(0.0001)
            clk.value(0)
            wr_data <<= 1
        return

    def start(self):
        clk = self.clk
        dio = self.dio

        dio.value(1)
        clk.value(1)
        time.sleep(0.0001)
        dio.value(0)
        return
        
    def ack(self):
        clk = self.clk
        dio = self.dio
        dy = 0
        clk.value(0)
        time.sleep(0.0001)
        dio = Pin(self.dioPin, Pin.IN)
        while(dio.value() == 1):
            time.sleep(0.0001)
            dy += 1
            if(dy>5000):
                break
        clk.value(1)
        time.sleep(0.0001)
        clk.value(0)
        dio = Pin(self.dioPin, Pin.OUT)
        return 
        
    def stop(self):
        clk = self.clk
        dio = self.dio
        dio.value(0)
        clk.value(1)
        time.sleep(0.0001)
        dio.value(1)
        return
        
    def displayBit(self,bit, num):
        if(num > 9 and bit > 4):
            return
        self.start()
        self.writeByte(self.ADDR_DIS)
        self.ack()
        self.writeByte(self.DisplayCommand)
        self.ack()
        self.stop()
        self.start()
        self.writeByte(self.DIG[bit-1])
        self.ack()
        if(self.DOT[bit-1] == 1):
            self.writeByte(self.NUM[num] | 0x80)
        else:
            self.writeByte(self.NUM[num])
        self.ack()
        self.stop()
        return
        
    def clearBit(self,bit):
        if(bit > 4):
            return
        self.start()
        self.writeByte(self.ADDR_DIS)
        self.ack()
        self.writeByte(self.DisplayCommand)
        self.ack()
        self.stop()
        self.start()
        self.writeByte(self.DIG[bit-1])
        self.ack()
        self.writeByte(0x00)
        self.ack()
        self.stop()
        return
        
        
    def setBrightness(self,b = 2):
        self.DisplayCommand = (self.DisplayCommand & 0x0f)+(b<<4)
        return

    def setMode(self,segment = 0):
        self.DisplayCommand = (self.DisplayCommand & 0xf7)+(segment<<3)
        return
        
    def displayOnOFF(self,OnOff = 1):
        self.DisplayCommand = (self.DisplayCommand & 0xfe)+OnOff
        return

    def displayDot(self,bit, OnOff):
        if(bit > 4):
            return
        if(OnOff == 1): 
            self.DOT[bit-1] = 1;
        else:
            self.DOT[bit-1] = 0;
        return
            

    def ShowNum(self,num): #0~9999
        self.displayBit(1,num%10)
        if(num < 10):
            self.clearBit(2)
            self.clearBit(3)
            self.clearBit(4)
        if(num > 9 and num < 100):
            self.displayBit(2,num//10%10)
            self.clearBit(3)
            self.clearBit(4)
        if(num > 99 and num < 1000):
            self.displayBit(2,num//10%10)
            self.displayBit(3,num//100%10)
            self.clearBit(4)
        if(num > 999 and num < 10000):
            self.displayBit(2,num//10%10)
            self.displayBit(3,num//100%10)
            self.displayBit(4,num//1000)

    